package p1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Imbig implements Comparator<Integer> {
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}

public class TestHeap {
    public int[] elem;
    //存放有效数据的个数
    public int usedSize;

    public TestHeap() {
        elem = new int[10];
    }

    public void init(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            elem[i] = arr[i] ;
            usedSize++;
        }
    }

    public void createHeap() {
        for(int i = (usedSize-1-1) / 2 ; i  >= 0;i--) {
            siftDown(i,usedSize);
        }
    }

    private void siftDown(int parent,int usedSize) {
        int child = 2 * parent + 1;
        while(child < usedSize) {
            if(child + 1 < usedSize && elem[child] < elem[child + 1]) {
                child++;
            }
            if(elem[parent] < elem[child]) {
                int tem = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tem;

                parent = child;
                child = 2*parent +1;
            } else {
                break;
            }
        }
    }

    public void offer(int val) {
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        siftUp(usedSize);
        usedSize++;
    }
    private void siftUp(int child) {
        int parent = (child-1) / 2;
        while(parent >= 0) {
            if(elem[parent] < elem[child] ) {
                int ret = elem[parent];
                elem[parent] = elem[child];
                elem[child] = ret;

                child = parent;
                parent = (child-1) / 2;
            }else {
                break;
            }
        }
    }

    public void poll() {
        if(isEmpty()) {
            return;
        }

        int ret = elem[0];
        elem[0] = elem[usedSize-1];
        elem[usedSize-1] = ret;

        siftDown(0,usedSize-1);
        usedSize--;


    }

    public boolean isFull () {
        return usedSize == elem.length;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

//    public void headSort() {
//        PriorityQueue<Integer> list = new PriorityQueue<>(new Imbig());
//
//        while() {
//            int head = elem[0];
//            elem[0] = elem[usedSize-1];
//            elem[usedSize-1] = head;
//
//            siftDown(0,usedSize-1);
//
//        }


//    public static void selectSort(int[] array) {
//        for (int i = 0; i < array.length; i++) {
//            for(int j = i+1;i < array.length;j++) {
//                int j = ;
//                if(array[j] < array[i]) {
//                    int tem = array[i];
//                    array[i]  =array[j];
//                    array[j] = tem;
//                }
//            }
//        }
//    }

    public void headSort() {
        int end = usedSize-1;
        while(end > 0) {
            int tem =elem[0];
            elem[0] = elem[end];
            elem[end] = tem;

            siftDown(0,end);

            end--;
        }
    }

    }

